# -*- coding:utf-8 -*-
import unittest
#from db import *
import db
from MySQLet import MySQLet

class TestMySQLet(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.mysql = MySQLet()
        sql = '''
            DROP TABLE IF EXISTS `students`;
            CREATE TABLE IF NOT EXISTS `students` (
                  `id` int(11) NOT NULL AUTO_INCREMENT,
                  `name` char(8) NOT NULL,
                  `sex` char(5) NOT NULL,
                  `age` tinyint(3) unsigned NOT NULL,
                  `tel` char(13) DEFAULT '-',
                  PRIMARY KEY (`id`)
            ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;    
            '''
        cls.mysql.doSQL(sql)

    def test_insert(self):
        table = 'students'
        param = dict(name='koka', sex='man', age=18, tel='10000')
        lastid = self.mysql.insert(table, param)
        self.assertEqual(lastid, 2)

    def test_insertMany(self):
        table = 'students'
        fields = ('name', 'sex', 'age', 'tel')
        values = [('koka', 'man', 18, '10000'), ('akok', 'man', 20, '10010'), ("hehe", "man", 11, "110"), ("xixi", "falme", 12, "112")]

        params = []
        [params.append(dict(zip(fields,value))) for value in values]
        count = self.mysql.insertMany(table, params)
        self.assertEqual(count, 4)

    @classmethod
    def tearDownClass(cls):
        # self.conn.close()
        cls.mysql.close()

def suite():
    tests = ['test_insert', 'test_insertMany']
    return unittest.TestSuite(map(TestMySQLet, tests))


if __name__ == '__main__':
    #创建测试容器
    # suite = unittest.TestSuite()
    # #添加测试用例
    # suite.addTest(TestMySQLet('test_insert'))
    # suite.addTest(TestMySQLet('test_insertMany'))
    # runner = unittest.TextTestRunner()
    # runner.run(suite)
    unittest.main(defaultTest='suite')
